package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Resource
    private AddressBookMapper addressBookMapper;
    //查询当前用户下的全部地址
    @Override
    public List<AddressBook> selectAllAddressBookByUserId() {
        List<AddressBook> addressBookList = addressBookMapper.selectAllAddressBookByUserId(BaseContext.getCurrentId());
        return addressBookList;
    }
    //新增地址
    @Override
    public void insertAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insertAddressBook(addressBook);
    }
    //根据id查询
    @Override
    public AddressBook selectById(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        return addressBook;
    }
    //根据id修改地址
    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBookMapper.UpdateAddressBook(addressBook);
    }
    //很根据id删除
    @Override
    public void DeleteAddressBookById(Long id) {
        addressBookMapper.DeleteAddressBookById(id);
    }
    //设置默认地址
    @Override
    @Transactional
    public void SetDefaultAddressBook(AddressBook addressBook) {

        addressBookMapper.SetDefault(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        addressBookMapper.SetDefaultAddressBook(addressBook);

    }
}
